package ui;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import model.LogisticsRequirement;
import model.vo.QueryRequirementVO;
import service.LogisticsRequirementService;
import utils.DBUtil;
import utils.ExcelExportUtil;
import utils.ExcelImportUtil;
import utils.ExcelLoadUtil;

public class RequirementListPage {
    private Stage stage;
    private LogisticsRequirementService service;

    public RequirementListPage(Stage stage) {
        this.stage = stage;
        this.service = new LogisticsRequirementService();
    }

    public void show() {
        VBox root = new VBox(40);
        root.setPadding(new Insets(50));
        root.setAlignment(Pos.CENTER);
        root.setStyle("-fx-background-color: white;");

        // 标题
        Label titleLabel = new Label("查询物流运输要求");
        titleLabel.setStyle("-fx-font-size: 32px; -fx-font-weight: bold;");


        // 表格
        TableView<QueryRequirementVO> table = new TableView<>();
        table.setPrefHeight(500);
        table.setStyle("-fx-font-size: 16px;");

        // 定义列
        TableColumn<QueryRequirementVO, String> serialCol = new TableColumn<>("序号");
        serialCol.setPrefWidth(50);
        serialCol.setCellValueFactory(new PropertyValueFactory<>("serial_no_str"));

        TableColumn<QueryRequirementVO, String> itemCol = new TableColumn<>("项目");
        itemCol.setPrefWidth(200);
        itemCol.setCellValueFactory(new PropertyValueFactory<>("item"));

        TableColumn<QueryRequirementVO, String> requirementCol = new TableColumn<>("具体要求");
        requirementCol.setPrefWidth(400);
        requirementCol.setCellValueFactory(new PropertyValueFactory<>("requirement"));

        TableColumn<QueryRequirementVO, String> proEffiCol = new TableColumn<>("处理时效");
        proEffiCol.setPrefWidth(150);
        proEffiCol.setCellValueFactory(new PropertyValueFactory<>("proEffi"));

        TableColumn<QueryRequirementVO, String> responsibleCol = new TableColumn<>("责任人");
        responsibleCol.setPrefWidth(150);
        responsibleCol.setCellValueFactory(new PropertyValueFactory<>("responsiblePerson"));

        TableColumn<QueryRequirementVO, String> supervisorCol = new TableColumn<>("监管人");
        supervisorCol.setPrefWidth(150);
        supervisorCol.setCellValueFactory(new PropertyValueFactory<>("supervisor"));

        // 设置表格列的样式
        String columnStyle = "-fx-font-size: 16px; -fx-alignment: CENTER;";
        serialCol.setStyle(columnStyle);
        itemCol.setStyle(columnStyle);
        requirementCol.setStyle(columnStyle);
        responsibleCol.setStyle(columnStyle);
        supervisorCol.setStyle(columnStyle);

        // 在定义其他列的代码后面添加删除按钮列
        TableColumn<QueryRequirementVO, Void> actionCol = new TableColumn<QueryRequirementVO, Void>("操作");
        actionCol.setPrefWidth(200);
        actionCol.setStyle(columnStyle);

        // 加载数据
        table.setItems(service.getAllRequirements());

// 设置单元格工厂创建删除按钮
        actionCol.setCellFactory(new Callback<TableColumn<QueryRequirementVO, Void>, TableCell<QueryRequirementVO, Void>>() {
            @Override
            public TableCell<QueryRequirementVO, Void> call(TableColumn<QueryRequirementVO, Void> param) {
                return new TableCell<QueryRequirementVO, Void>() {
                    private final HBox buttonBox = new HBox(20);
                    private final Button deleteButton = new Button("删除");// 按钮容器，间距10
                    private final Button editButton = new Button("修改");
                    {
                        // 设置按钮样式
                        editButton.getStyleClass().add("edit-button");
                        deleteButton.getStyleClass().add("delete-button");
                        // 设置按钮大小
                        editButton.setPrefWidth(60);
                        deleteButton.setPrefWidth(60);
                        // 添加按钮到容器
                        buttonBox.getChildren().addAll(editButton, deleteButton);
                        buttonBox.setAlignment(Pos.CENTER);
                        editButton.setOnAction(event -> {
                            QueryRequirementVO data = getTableView().getItems().get(getIndex());
                            // 打开修改界面
                            UpdateRequirementPage editPage = new UpdateRequirementPage(new Stage(), data);
                            editPage.show();
                            stage.close();
                        });
                        deleteButton.setOnAction(event -> {
                            QueryRequirementVO data = getTableView().getItems().get(getIndex());
                            // 调用删除逻辑
                            LogisticsRequirementService service = new LogisticsRequirementService();
                            service.deleteRequirement(data.getSerial_no());
                            // 刷新表格
                            table.setItems(service.getAllRequirements());
                        });
                    }

                    @Override
                    protected void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(buttonBox);
                        }
                    }
                };
            }
        });

        HBox buttonBox = new HBox(20); // 设置按钮间距为20
        buttonBox.setAlignment(Pos.CENTER); // 居中对齐
        table.getColumns().addAll(serialCol, itemCol, requirementCol, responsibleCol, proEffiCol,supervisorCol,actionCol);

        Button addButton = new Button("添加");
        setupButton(addButton, "add-button");
        addButton.setOnAction(e -> {
            AddRequirementPage addRequirementPage = new AddRequirementPage(new Stage());
            try {
                addRequirementPage.show();
                stage.close();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        Button importButton = new Button("导入");
        setupButton(importButton, "import-button");
        importButton.setOnAction(e -> {
            ExcelImportUtil.readRequirement();
            RequirementListPage requirementListPage = new RequirementListPage(new Stage());
            requirementListPage.show();
            stage.close();
        });


        // 返回按钮
        Button backButton = new Button("返回");
        setupButton(backButton, "back-button");
        backButton.setOnAction(e -> {
            MenuPage menuPage = new MenuPage();
            try {
                menuPage.start(new Stage());
                stage.close();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        Button excelButton = new Button("导出Excel");
        setupButton(excelButton, "excel-button");
        excelButton.setOnAction(e -> {
            ExcelExportUtil.exportRequirement(service.getAllRequirements());
        });

        //下载模板
        Button loadButton = new Button("下载模板");
        loadButton.setOnAction(e -> {
            ExcelLoadUtil.loadRequirementExcel();
        });
        loadButton.getStyleClass().add("load-button");
        buttonBox.getChildren().addAll(addButton, loadButton, importButton, excelButton, backButton);

        root.getChildren().addAll(titleLabel, table, buttonBox);

        Scene scene = new Scene(root, 1300, 900);
        scene.getStylesheets().add(getClass().getResource("/css/requirementList.css").toExternalForm());
        
        stage.setScene(scene);
        stage.setTitle("查询物流运输要求");
        stage.show();
    }

    private void setupButton(Button button, String styleClass) {
        button.getStyleClass().add(styleClass);
        button.setStyle("-fx-font-size: 16px; -fx-padding: 10 30;");
        button.setPrefWidth(150);
        button.setPrefHeight(45);
    }
} 